//
//  CZRefreshView.swift
//  sinaSwifWeiBo
//
//  Created by 张雪雪 on 16/3/8.
//  Copyright © 2016年 张雪雪. All rights reserved.
//

import UIKit
import SnapKit

//分析三个状态

enum RefreshType: Int {

   //正常的状态
    case Normal = 0
    //拖拉状态
    case Pulling = 1
    //加载状态
    case Loading = 2

}

class CZRefreshView: UIControl {
    
    //成员变量
    var scrollView: UIScrollView?
    
    //记录一下过去的状态
    var oldStatus: RefreshType = .Normal
    
    //定义一个属性来记录状态，默认是正常状态
    var status: RefreshType = .Normal{
    
        didSet{
        
            switch status{
            
            case .Normal: print("正常状态")
                
                //文字
                messageLabel.text = "下拉刷新"
                
            case .Pulling:print("下拉状态")
                
                messageLabel.text = "释放刷新"
                
            case .Loading:print("加载中......")
            
                messageLabel.text = "加载中"
            
                var inset = scrollView?.contentInset
                
                inset?.top += 64
                
                scrollView?.contentInset = inset!
            
            //没有触发事件
                sendActionsForControlEvents(UIControlEvents.ValueChanged)
              //记录加载的状态
                oldStatus = .Loading
                
               //需要回复回去
            }
        
        }
    
    
    }
    
    func endRefreshing(){
    
    //加载完成
        status = .Normal
        
        if oldStatus == .Loading{
        
        //加载的时候让图片显示在nav下面 修改scrollView?.contentInset
            var inset = scrollView?.contentInset
            
            inset?.top -= 64
            scrollView?.contentInset = inset!
            //恢复标记
            oldStatus = .Normal
            
        }
    
    
    
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        backgroundColor = UIColor.yellowColor()
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
    //设置frame 
    //获取系统的frame
        
        var frame = self.frame
        //修改frame
        frame.size = CGSizeMake(kUIScreenWidth, 50)
        
        frame.origin.y = -50
        
        //赋值
        self.frame = frame
        
        //添加视图约束
        addSubview(messageLabel)
        addSubview(arrowImageView)
        addSubview(loadingImageView)
        
        //添加约束
        //水平居中,汪右30个点，垂直居中
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX).offset(30)
            make.centerY.equalTo(self.snp_centerY)
        }
        
        //messageLable的左边,水平居中
        arrowImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp_centerY)
            make.right.equalTo(messageLabel.snp_left)
        }
        
        loadingImageView.snp_makeConstraints { (make) -> Void in
            //注意make.center.equalTo(arrowImageView.center)不一样
            make.center.equalTo(arrowImageView.snp_center)
        }
        
    
    }
    //添加KVO
 //这个方法是,我们把子空间添加到父控件的时候会调用这个方法
    //newSuperview是UITableView
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(newSuperview)
        //1.先把newSuperview 转换成UIScrollView
        
        scrollView = newSuperview as? UIScrollView
        
        //2.添加监听
        scrollView?.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
        
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        let scorllView = object as? UIScrollView
        
        if scorllView!.dragging {//拖拽的时候再去判断我们的额状态
            if scorllView?.contentOffset.y < -150 {
            
            //改变状态
                status = .Pulling
                
                //调用一个方法去改变视图
                
                
            
            }else if scorllView?.contentOffset.y < -144 && scorllView?.contentOffset.y > -150{
            
            //记录状态
                status = .Normal
            
            }
        
        }else{//松手的时候
            
            
            if scorllView?.contentOffset.y < -150 && status == .Pulling{
            
            status = .Loading
            
            }

        }
        
    }
    
    
//懒加载
    private lazy var messageLabel : UILabel = {
    
    let lable = UILabel()
        
        lable.text = "下拉刷新"
        
        lable.textColor = UIColor.redColor()
        
        lable.font = UIFont.systemFontOfSize(15)
    
        return lable
    }()

    private lazy var arrowImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named:"tableview_pull_refresh")
    
    return img
    
    }()
    
    //加载视图
    private lazy var loadingImageView: UIImageView = {
        
    
        let img = UIImageView()
        
        img.image = UIImage(named:"tableview_loading")
    
        img.hidden = true
        
        return img
    }()
    
    

}









